using UnityEngine;
using System.Collections;

public class EnnemySound : MonoBehaviour {
	
	public AudioClip 	FireClip;
	
	public AudioClip[] 	HurtClip;
	private int lLastHurtGrabbed;
	
	public AudioClip	Destroy;
	
	private AudioSource fireSource;
	private AudioSource hurtSource;
	
	private AudioSource ambientSource;
	
	private GameObject SoloGo;
	
	// Use this for initialization
	void Start () 
	{
		SoloGo = new GameObject("Solo");
		SoloGo.transform.parent = transform;
		
		fireSource = SoloGo.AddComponent<AudioSource>();
		hurtSource = SoloGo.AddComponent<AudioSource>();
		
		lLastHurtGrabbed = -1;
	}
	
	public void OnFire()
	{
		fireSource.PlayOneShot(FireClip);
	}
	
	public void OnHurt()
	{
		int lRand = Random.Range(0, HurtClip.Length);
		
		if(lRand == lLastHurtGrabbed)
		{
			lRand++;
			if(lRand >= HurtClip.Length)
				lRand = 0;
		}
		
		if(!hurtSource.isPlaying)
			hurtSource.PlayOneShot(HurtClip[lRand]);
		
		lLastHurtGrabbed = lRand;
	}
	
	public void OnDetruit()
	{
		if(SoloGo == null)
			return;
		
		SoloGo.transform.parent = null;
		SoloGo.AddComponent<AutoDestruct>().SetSuicide(3.0f);
		hurtSource.volume = 0.2f;
		hurtSource.pitch = Random.Range(0.4f, 1.6f);
		hurtSource.PlayOneShot(Destroy);
	}
	
	public void SetAmbient(AudioClip pAmbient, float pPitchMin, float pPitchMax, float pVolume)
	{
		if(ambientSource == null)
			ambientSource = gameObject.AddComponent<AudioSource>();
		
		
		ambientSource.clip = pAmbient;
		ambientSource.loop = true;
		ambientSource.volume = pVolume;
		ambientSource.pitch = Random.Range(pPitchMin, pPitchMax);
		
		ambientSource.Play();
	}
}
